<!-- 注册
 1. vue始终在用双向绑定的思想来准备前后端交互，每一个vue都会定义好变量给 表单（form）或者表格（table）中的html元素的值绑定
-->
<template>
  <div><!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
    <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
    <el-dialog title="修改修改信息" :visible.sync="editDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
      <!--图片上传开始-->
      <div style="position: absolute;top:83px;left:325px;" v-show="oldImg">
        <img :src="getImgUrl(this.others.oimg)" style="width: 146px;height: 146px;border-radius: 4px"/>
      </div>
      <el-form label-width="120px" ref="editOthersUploadForm">
      <el-form-item label="请选择头像">
        <el-upload
          class="upload"
          :action="getUploadUrl"
          list-type="picture-card"
          :limit="1"
          accept="image/*"
          :on-change="(file,fileList)=>{imgHandleChange(file,fileList,1,this,'editOthersUploadForm','others')}"
          :on-remove="(file,fileList)=>{imgHandleRemove(1,this)}"
          :on-before="(file,fileList)=>{imghandleBeforeUpload(file,1,this)}"
          :auto-upload="false"
          :file-list="fileList[0]"
          ref="file1">
          <i class="el-icon-plus"></i>
        </el-upload>
        <!--<viewer-dialog ref="viewer" :images="imagesUp"></viewer-dialog>-->
      </el-form-item>
      <!-- 图片上传结束-->
      </el-form>
      <el-form :model="others" label-width="120px" :rules="editOthersRules" ref="editOthersForm">
        <el-form-item label="修改名称" prop="oname">
          <el-col :span="8">
            <el-input v-model="others.oname"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="修改账号" prop="oid">
          <el-col :span="8">
            <el-input v-model="others.oid"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="修改密码" prop="opwd">
          <el-col :span="8">
            <el-input v-model="others.opwd"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="修改地址" prop="oiddr">
          <el-col :span="15">
            <el-input v-model="others.oiddr"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span>
      <el-button type="success" @click="editOthers('editOthersForm')">修改</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import api from "../../../../axios/api"
  import rules from "../../../../static/js/validator/rules.js";
  import utils from "../../../../static/js/utils/upload.js"

  export default {
    name: "editOthers",
    data() {
      return {
        oldImg: false,
        //上传使用
        fileList: [[], [], []], //缓存区文件
        uploadFile: [[], [], []], //  上传用文件
        formData: {files: []},
        imagesUp: null,
        //上传使用结束
        others: {oname: "", oimg: "", oid: "", opwd: "",oiddr: ""},
        editDialogIsShow: false,
        allOthersGroup: [{num: 0, "astate": "忙碌"}, {num: 1, "astate": "空闲"}],
        editOthersRules: {
          oname: [{required: true, message: "名称不能为空", trigger: 'blur'},
            {max: 20, min: 2, message: '长度异常 长度在2-20之间', trigger: 'blur'}, {
              pattern: /^[\u0391-\uFFE5A-Za-z]+$/,
              message: '名称必须为中文',
              trigger: 'blur'
            }],
          oid: [{required: true, message: "ID不能为空", trigger: 'blur'}, {
            max: 11,
            min: 2,
            message: '长度异常',
            trigger: 'blur'
          }, {pattern: /^\d+$|^\d+[.]?\d+$/, message: '必须为数字', trigger: 'blur'}],
          opwd: [{required: true, message: "密码不能为空", trigger: 'blur'}, {
            max: 11,
            min: 2,
            message: '长度异常',
            trigger: 'blur'
          }, {
            pattern: /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,20}$/,
            message: '仅由英文字母，数字以及下划线组成',
            trigger: 'blur'
          }],
          oiddr: [{required: true, message: "地址不能为空", trigger: 'blur'}]
        }
      }
    },
    methods: {
      getImgUrl: function (oimg) {
        return this.$store.state.host + "/upload/images/others/" + oimg;
      },
      editOthers: function (editOthersForm) {
        this.$refs[editOthersForm].validate(valid => {//此处refs对象和标签上的ref对应
          if (valid) {
            //如果通过验证 to do...
            //console.log(this.others);
            this.setDataFromAxios();
            alert(this.others.oname + "添加成功");
            this.handleClose();

          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      //页面初始化时加载的事件
      init(others) {
        this.fileList = [[], [], []] //缓存区文件
        this.uploadFile = [[], [], []],
          this.editDialogIsShow = true;
        this.others = others;
        console.log(others);
        if (this.others.oimg != null && this.others.oimg != '') {
          this.oldImg = true;
        }
      },

      //关闭页面
      handleClose() {
        this.editDialogIsShow = false;
        //给父组件传参
        this.$emit("editDialogIsShow", this.editDialogIsShow);
      },
      setDataFromAxios: function () {
        alert(this.$store.state.host);
        console.log(this.others);
        api.setDataFromAxios(this.$store.state.host + "othersController/updateOthersByOno", this.others, "post")
          .then(res => {
            this.oldImg = false;
            this.leagues = res.leagues
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      },
      imghandleBeforeUpload(file, type, vm) {

        return utils.UPLOAD.Upload().handleBeforeUpload(file, type, vm);
      },
      imgHandleChange(file, fileList, type, vm, modelName, name) {
        if (vm.fileList[type].length != 0) {
          alert("请先删除再提交")
        }
        return utils.UPLOAD.Upload().handleChange(file, fileList, type, vm, modelName, name);
      },
      imgHandleRemove(type, vm) {

        return utils.UPLOAD.Upload().handleRemove(type, vm);
      },
      uploadImg: function (modelName, name) {
        //  使用formdata格式
        let formData = new FormData();
        formData.append("model", modelName);//图片上传子目录
        console.log(formData);
        console.log(this.formData.files[name]);
        if (this.formData.files[name]) {
          formData.append("file", this.formData.files[name]);
        }
        console.log("----------------------------");
        console.log(formData);
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
        api.setDataFromAxios(this.$store.state.host + "/uploadImg", formData, "post", {headers: {"Content-Type": "multipart/form-data"}})
          .then(dto => {
            this.oldImg = false;
            if (dto.code == 200) {
              this.others.oimg = dto.obj;
              this.$message.success("上传图片成功!");
            } else {
              this.$message.error("上传图片失败!");
            }
          });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      },
    },
    computed: {
      getUploadUrl() {
        return this.$store.state.host + "/uploadImg";
      }
    },
    //监听
    watch: {
      dialogisShow(newValue, oldValue) {
        this.editDialogIsShow = newValue;
      }
    }

  }
</script>

<style scoped>

</style>
